//
//  BindPhoneViewController.m
//  reSearchDemo
//
//  Created by helfy  on 15-4-17.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "BindPhoneViewController.h"

@interface BindPhoneViewController ()
{
    UITextField *textFiled;
    
    UITextField *codeTextFiled;
    UIButton *getCodeButton ;
   
    UIButton *saveButton;
    
    UILabel *numberLabel;
    
    
    NSTimer *timer;
    int time;
}
@end

@implementation BindPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"手机绑定";
    // Do any additional setup after loading the view.
    
    [self setBindedUI];
}

-(void)setUnBindUI
{
    if(textFiled == nil)
    {
        textFiled = [UITextField new];
        
    }
    textFiled.enabled = YES;
    [self.view addSubview:textFiled];
    textFiled.textAlignment = NSTextAlignmentCenter;
    textFiled.text = nil;
    textFiled.borderStyle = UITextBorderStyleLine;
    textFiled.placeholder = @"请输入手机号码";
    
    [textFiled mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(100);
        make.left.equalTo(10);
        make.right.equalTo(-10);
        make.height.equalTo(45);
    }];
    
    
    codeTextFiled = [UITextField new];
    [self.view addSubview:codeTextFiled];
    codeTextFiled.borderStyle = UITextBorderStyleLine;
    codeTextFiled.placeholder = @"验证码";
    [codeTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textFiled.mas_bottom).equalTo(10);
        make.left.equalTo(10);
       make.width.equalTo(100);
        make.height.equalTo(45);
    }];
    
    getCodeButton = [UIButton new];
    [self.view addSubview:getCodeButton];
    [getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [getCodeButton addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
    
    getCodeButton.backgroundColor = [UIColor grayColor];
    [getCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textFiled.mas_bottom).equalTo(10);
        make.right.equalTo(-10);
        make.height.equalTo(45);
        make.width.equalTo(100);
    }];
    
    if(saveButton == nil)
    {
        saveButton = [UIButton new];
    }
  
    [self.view addSubview:saveButton];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [saveButton removeTarget:self action:@selector(changePhone) forControlEvents:UIControlEventTouchUpInside];
    [saveButton addTarget:self action:@selector(setPhone) forControlEvents:UIControlEventTouchUpInside];


    saveButton.backgroundColor = [UIColor grayColor];
    [saveButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(codeTextFiled.mas_bottom).equalTo(40);
        make.left.equalTo(10);
        make.right.equalTo(-10);
        make.height.equalTo(45);
    }];
    
}

-(void)setBindedUI
{
    
    [codeTextFiled removeFromSuperview];
    [getCodeButton removeFromSuperview];
    if(textFiled == nil)
    {
        textFiled = [UITextField new];
    }
    
    [self.view addSubview:textFiled];
    textFiled.borderStyle = UITextBorderStyleNone;
    textFiled.textAlignment = NSTextAlignmentCenter;
//    textFiled.text = @"13408645740";
    textFiled.enabled = NO;
    [textFiled mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(100);
        make.left.equalTo(10);
        make.right.equalTo(-10);
        make.height.equalTo(45);
    }];
    saveButton = [UIButton new];
    [self.view addSubview:saveButton];
    [saveButton setTitle:@"更换号码" forState:UIControlStateNormal];

    [saveButton removeTarget:self action:@selector(setPhone) forControlEvents:UIControlEventTouchUpInside];
    [saveButton addTarget:self action:@selector(changePhone) forControlEvents:UIControlEventTouchUpInside];
    saveButton.backgroundColor = [UIColor grayColor];
    [saveButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textFiled.mas_bottom).equalTo(40);
        make.left.equalTo(10);
        make.right.equalTo(-10);
        make.height.equalTo(45);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)repeat:(NSTimer*)tr
{
    if (time <= 0) {
        getCodeButton.enabled = YES;
        [getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        if (timer) {
            [timer invalidate];
            timer = nil;
        }
    } else {
        [getCodeButton setTitle:[NSString stringWithFormat:@"剩余%d秒", time] forState:UIControlStateDisabled];
        time -= 1;
    }
}



-(void)changePhone
{
    [self setUnBindUI];
}
-(void)getCode
{
    if (textFiled.text.length != 11) {
        [self showText:@"请输入正确的手机号"];
    } else {
        if (client) {
            return;
        }
        [self setLoading:YES content:@"正在发送验证码"];
        client = [[BSClient alloc] initWithDelegate:self action:@selector(requestDidCodeFinish:obj:)];
        [client getPhoneCode:textFiled.text];
        [self resignAllKeyboard:self.view];
    }

}
- (BOOL)requestDidCodeFinish:(id)sender obj:(NSDictionary *)obj {
    if ([super requestDidFinish:sender obj:obj]) {
//        NSDictionary * data = [obj getDictionaryForKey:@"data"];
//        NSString * code = [data getStringValueForKey:@"code" defaultValue:@""];
        [self showAlert:@"验证码已经发送到你的手机，请注意查收！" isNeedCancel:NO];
//        codeTextFiled.text = code;
        getCodeButton.enabled = NO;
        if (timer) {
            [timer invalidate];
            timer = nil;
        }
        time = 60;
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(repeat:) userInfo:nil repeats:YES];
        [self repeat:nil];
    }
    return YES;
}


-(void)setPhone
{
    [self setBindedUI];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
